package xyz.doikki.dkplayer.fragment;


import android.content.Intent;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.ViewPager;
import xyz.doikki.dkplayer.R;
import xyz.doikki.dkplayer.adapter.Tiktok2Adapter;
import xyz.doikki.dkplayer.bean.TiktokBean;
import xyz.doikki.dkplayer.util.DataUtil;
import xyz.doikki.dkplayer.util.Utils;
import xyz.doikki.dkplayer.util.cache.PreloadManager;
import xyz.doikki.dkplayer.util.cache.ProxyVideoCacheManager;
import xyz.doikki.dkplayer.widget.VerticalViewPager;
import xyz.doikki.dkplayer.widget.controller.TikTokController02;
import xyz.doikki.dkplayer.widget.render.TikTokRenderViewFactory;
import xyz.doikki.videoplayer.player.VideoView;
import xyz.doikki.videoplayer.util.L;

/**
 * Created by 聪明一只哈 on 2024-01-12.
 * Time: 2024-01-12  11:37
 */

public class TokFragment extends BaseFragment {


  /**
   * 当前播放位置
   */
  private int mCurPos;
  private List<TiktokBean> mVideoList = new ArrayList<>();
  private Tiktok2Adapter mTiktok2Adapter;
  private VerticalViewPager mViewPager;

  private PreloadManager mPreloadManager;

  private TikTokController02 mController;

  private static final String KEY_INDEX = "index";
  private VideoView mVideoView;


  @Override
  protected int getLayoutResId() {
    return R.layout.activity_tiktok_fg;
  }

  @Override
  protected void initView() {
    super.initView();
    initViewPager();
    initVideoView();
    mPreloadManager = PreloadManager.getInstance(getContext());

    addData(null);
    int index = 0;
    mViewPager.setCurrentItem(index);

    mViewPager.post(new Runnable() {
      @Override
      public void run() {
        startPlay(index);
      }
    });
  }


  private void initVideoView() {
    mVideoView = new VideoView(getContext());
    mVideoView.setLooping(true);

    //以下只能二选一，看你的需求
    mVideoView.setRenderViewFactory(TikTokRenderViewFactory.create());
//        mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_CENTER_CROP);

    mController = new TikTokController02(getContext());
    mVideoView.setVideoController(mController);
  }

  private void initViewPager() {
    mViewPager = findViewById(R.id.vvp);
    mViewPager.setOffscreenPageLimit(4);
    mTiktok2Adapter = new Tiktok2Adapter(mVideoList);
    mViewPager.setAdapter(mTiktok2Adapter);
    mViewPager.setOverScrollMode(View.OVER_SCROLL_NEVER);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

      private int mCurItem;

      /**
       * VerticalViewPager是否反向滑动
       */
      private boolean mIsReverseScroll;

      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        Log.d("TAG_Tiktok2", "onPageScrolled: "+position+"~~~~"+mCurItem);
        if (position == mCurItem) {
          return;
        }
        mIsReverseScroll = position < mCurItem;
      }

      @Override
      public void onPageSelected(int position) {
        super.onPageSelected(position);
        if (position == mCurPos) return;
        startPlay(position);
      }

      @Override
      public void onPageScrollStateChanged(int state) {
        super.onPageScrollStateChanged(state);
        if (state == VerticalViewPager.SCROLL_STATE_DRAGGING) {
          mCurItem = mViewPager.getCurrentItem();
        }

        if (state == VerticalViewPager.SCROLL_STATE_IDLE) {
          mPreloadManager.resumePreload(mCurPos, mIsReverseScroll);
        } else {
          mPreloadManager.pausePreload(mCurPos, mIsReverseScroll);
        }
      }
    });
  }

  private void startPlay(int position) {
    int count = mViewPager.getChildCount();
    for (int i = 0; i < count; i ++) {
      View itemView = mViewPager.getChildAt(i);
      Tiktok2Adapter.ViewHolder viewHolder = (Tiktok2Adapter.ViewHolder) itemView.getTag();
      if (viewHolder.mPosition == position) {
        mVideoView.release();
        Utils.removeViewFormParent(mVideoView);

        TiktokBean tiktokBean = mVideoList.get(position);
        String playUrl = mPreloadManager.getPlayUrl(tiktokBean.videoDownloadUrl);
        L.i("startPlay: " + "position: " + position + "  url: " + playUrl);
        mVideoView.setUrl(playUrl);
        //请点进去看isDissociate的解释
        mController.addControlComponent(viewHolder.mTikTokView, true);
        viewHolder.mPlayerContainer.addView(mVideoView, 0);




        mVideoView.start();
        mCurPos = position;
        break;
      }
    }
  }

  public void addData(View view) {
    mVideoList.addAll(DataUtil.getTiktokDataFromAssets(getContext()));
    mTiktok2Adapter.notifyDataSetChanged();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    mPreloadManager.removeAllPreloadTask();
    //清除缓存，实际使用可以不需要清除，这里为了方便测试
    ProxyVideoCacheManager.clearAllCache(getContext());
  }



}
